package com.atguigu.tingshu.cdc.album;

import com.atguigu.tingshu.cdc.user.UserCDC;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * Change Data Capture数据变更捕获
 *
 */
@Slf4j
@Component
@CanalTable("album_info") //监听变更表
public class AlbumCdcHandler implements EntryHandler<AlbumCDC> {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增操作：
     *
     * @param userCDC 新增记录封装Java对象
     */
    @Override
    public void insert(AlbumCDC userCDC) {
        log.info("监听到Canal服务端新增操作：{}", userCDC);
    }

    /**
     * 更新操作
     *
     * @param before 更新前记录封装Java对象
     * @param after  更新后记录封装Java对象
     */
    @Override
    public void update(AlbumCDC before, AlbumCDC after) {
        log.info("监听到Canal服务端修改操作：{}", after);
        redisTemplate.delete("album:info:" + after.getId());
        //TODO 更新ElasticSearch索引库(之前自动上下架索引库会读到redis旧数据)
    }

    /**
     * 删除操作
     *
     * @param userCDC 删除前记录
     */
    @Override
    public void delete(AlbumCDC userCDC) {
        log.info("监听到Canal服务端删除操作：{}", userCDC);
        redisTemplate.delete("albumInfo:" + userCDC.getId());
    }
}
